package com.google.code.virtualhockey.vhx.client.vp;

import com.google.code.virtualhockey.vhx.client.VhxEntryPoint;
import com.google.code.virtualhockey.vhx.client.res.VhxResources;
import com.google.code.virtualhockey.vhx.client.uitools.IView;
import com.google.gwt.core.client.GWT;
import com.google.gwt.resources.client.ClientBundle;
import com.google.gwt.uibinder.client.UiBinder;
import com.google.gwt.uibinder.client.UiField;
import com.google.gwt.user.client.ui.*;
import com.google.inject.Inject;

/** Main view representing the application main window. */
public class MainView extends Composite implements IView {

  interface Binder extends UiBinder<DockLayoutPanel, MainView> {
  }

  private static final Binder BINDER = GWT.create( Binder.class );

  @UiField( provided = true )
  final HeaderView m_viewHeader;

  @UiField( provided = true )
  final ContentView m_viewContent;

  @UiField( provided = true )
  final MenuView m_viewMenu;

  @UiField( provided = true )
  final SplitLayoutPanel m_panelSplitter;

  @UiField( provided = true )
  final VhxResources m_res = VhxResources.INSTANCE;

  @UiField
  InlineLabel m_lblUser;

  @UiField
  Anchor m_aLogin;

  @UiField
  Anchor m_aLogout;

  /** Default parameterless constructor required by each view. */
  @Inject
  public MainView( HeaderView header, ContentView content, MenuView menu ) {
    m_viewHeader = header;
    m_viewContent = content;
    m_viewMenu = menu;
    m_panelSplitter = new SplitLayoutPanel( 4 );
    initWidget( BINDER.createAndBindUi( this ) );

    m_aLogin.setHref( VhxEntryPoint.getLoginUrl() );
    m_aLogin.setVisible( true );
    m_aLogout.setHref( VhxEntryPoint.getLogoutUrl() );
    m_aLogout.setVisible( true );
  }

  public Widget getViewWidget() {
    return this;
  }

  /**
   * Sets the currently connected user (if logged in).
   *
   * @param strUser connected user or null if none is authenticated
   */
  void setUser( String strUser ) {
    m_aLogin.setVisible( strUser == null );
    m_aLogout.setVisible( strUser != null );

    if ( strUser == null )
      m_lblUser.setText( "" );
    else if ( VhxEntryPoint.isAdmin() )
      m_lblUser.setText( strUser + " (Admin)" );
    else
      m_lblUser.setText( strUser );
  }
}
